/*
 * Copyright (c) 2015, Freescale Semiconductor, Inc.
 * Copyright 2016-2017 NXP
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* FreeRTOS kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "timers.h"

/* Freescale includes. */
#include "fsl_device_registers.h"
#include "fsl_debug_console.h"
#include "board.h"

#include "pin_mux.h"
#include "clock_config.h"

#include "common.h"

SemaphoreHandle_t I2c_xSemaphore = NULL;

void GPIO4_Combined_0_15_IRQHandler(void)
{
    //BaseType_t WTF = ( ( BaseType_t ) 1 );

    if(GPIO_PinRead(GPIO4,9U) == 1)
    {
        /* clear the interrupt status */
        PRINTF("GPIO4_09 interrupt.\r\n");
        
		if(Start_Signal == 0)
		{
			Start_Signal = 1;
		}
        if(Start_Signal == 2)
        {
            Crash_L = 1;
        }

        //vTaskNotifyGiveFromISR(Collision_Detection_task, &WTF);
        GPIO_PortClearInterruptFlags(GPIO4, 1U << 9U);
    }
    
    if(GPIO_PinRead(GPIO4,10U) == 1)
    {
        /* clear the interrupt status */
        PRINTF("GPIO4_10 interrupt.\r\n");
        
		if(Start_Signal == 0)
		{
			Start_Signal = 1;
		}
        if(Start_Signal == 2)
        {
            Crash_R = 1;
        }
        //xTaskNotifyFromISR(Collision_Detection_task,);
        //vTaskNotifyGiveFromISR(Collision_Detection_task, &WTF);
        GPIO_PortClearInterruptFlags(GPIO4, 1U << 10U);
    }

    /* Change state of switch. */
    //g_InputSignal = true;
    /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
      exception return operation might vector to incorrect interrupt */
#if defined __CORTEX_M && (__CORTEX_M == 4U)
    __DSB();
#endif
}


/*******************************************************************************
 * Code
 ******************************************************************************/
/*!
 * @brief Application entry point.
 */
int main(void)
{
    /* Init board hardware. */
    BOARD_ConfigMPU();
    BOARD_InitPins();
    BOARD_BootClockRUN();
    BOARD_InitDebugConsole();

    /* Enable clock gate for GPIO1 */
    CLOCK_EnableClock(kCLOCK_Gpio1);
    CLOCK_EnableClock(kCLOCK_Gpio2);

    /*Clock setting for LPI2C*/
    CLOCK_SetMux(kCLOCK_Lpi2cMux, LPI2C_CLOCK_SOURCE_SELECT);
    CLOCK_SetDiv(kCLOCK_Lpi2cDiv, LPI2C_CLOCK_SOURCE_DIVIDER);
    
    /* Init Output GPIO */
    Collision_Detect_Init();
    USER_LED_INIT(LOGIC_LED_OFF);

    L_MOTOR_DIR_INIT(Motor_Forward);
    R_MOTOR_DIR_INIT(Motor_Forward);
    Brush_MOTOR_DIR_INIT(Motor_Forward);
    Turbo_MOTOR_DIR_INIT(Motor_Forward);

    DIST_LED_INIT(LOGIC_LED_OFF);
    IR_SENSOR_INIT();
    PWM_DRV_Init();

    // I2c_xSemaphore = xSemaphoreCreateMutex();

    if (xTaskCreate(hello_task, "Hello_task", configMINIMAL_STACK_SIZE + 10, NULL, hello_task_PRIORITY, NULL) != pdPASS)
    {
        PRINTF("Task creation failed!.\r\n");
        while (1);
    }
    vTaskStartScheduler();
    for (;;)
        ;
}

